package com.lt.flashcard.model.entity;

// Generated Nov 23, 2012 2:01:25 PM by Hibernate Tools 4.0.0

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang3.StringUtils;

/**
 * Orderdetail generated by hbm2java
 */
@Entity
@Table(name = "orderdetail", catalog = "flashcard", uniqueConstraints = @UniqueConstraint(columnNames = "code"))
public class Orderdetail implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private User user;
    private Date created = new Date();
    private String userip = "";
    private int status = STATUS_DRAFT;
    public static final int STATUS_DRAFT = 0;
    /**
     * Waiting for the payment
     */
    public static final int STATUS_PAYMENT = 1;
    /**
     * The payment is complete and verified
     */
    public static final int STATUS_COMPLETE = 2;
    public static final int STATUS_PAYMENTERROR = 3;
    /**
     * User has cancel the payment
     */
    public static final int STATUS_CANCEL = 4;
    private int pricetotal = 0;
    private int cointotal = 0;
    private int paymenttype = TYPE_COIN;
    public static final int TYPE_COIN = 0;
    public static final int TYPE_NGANLUONG = 1;
    public static final int TYPE_CARD = 2;
    public static final int TYPE_CODE = 3;
    public static final int TYPE_OTHER = 4;
    private int paymentstatus = 0;
    private String paymenthistory = "";
    private String code;
    /**
     * The transaction id from NganLuong
     */
    private String transactionid = "";
    private Set<Orderproduct> orderproducts = new HashSet<Orderproduct>(0);

    public Orderdetail() {
    }

    public Orderdetail(User user, int pricetotal, int cointotal, int paymenttype) {
        this.user = user;
        this.pricetotal = pricetotal;
        this.cointotal = cointotal;
        this.paymenttype = paymenttype;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created", nullable = false, length = 0)
    public Date getCreated() {
        return this.created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Column(name = "userip", nullable = false, length = 20)
    public String getUserip() {
        return this.userip;
    }

    public void setUserip(String userip) {
        this.userip = userip;
    }

    @Column(name = "status", nullable = false)
    public int getStatus() {
        return this.status;
    }

    @Transient
    public String getStatusStr() {
        switch (status) {
        case STATUS_COMPLETE:
            return "Complete";
        case STATUS_DRAFT:
            return "Draft";
        case STATUS_PAYMENT:
            return "Wait for payment";
        case STATUS_PAYMENTERROR:
            return "Payment error";
        }
        return "Unknown";
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Column(name = "pricetotal", nullable = false)
    public int getPricetotal() {
        return this.pricetotal;
    }

    public void setPricetotal(int pricetotal) {
        this.pricetotal = pricetotal;
    }

    @Column(name = "paymenttype", nullable = false)
    public int getPaymenttype() {
        return this.paymenttype;
    }

    public void setPaymenttype(int paymenttype) {
        this.paymenttype = paymenttype;
    }

    @Transient
    public String getPaymenttypeStr() {
        switch (paymenttype) {
        case TYPE_CARD:
            return "Thẻ cào điện thoại";
        case TYPE_COIN:
            return "Xu";
        case TYPE_NGANLUONG:
            return "Ngân Lượng";
        default:
            return "Chưa rõ";
        }
    }

    @Column(name = "paymenthistory", nullable = false, length = 65535)
    public String getPaymenthistory() {
        return this.paymenthistory;
    }

    public void setPaymenthistory(String paymenthistory) {
        this.paymenthistory = paymenthistory;
    }

    /**
     * Append a history
     * @param paymethistory
     */
    @Transient
    public void addPaymentHistory(String history) {
        if (StringUtils.isBlank(history)) {
            return;
        }
        if (this.paymenthistory == null) {
            this.paymenthistory = "";
        }
        this.paymenthistory.concat("\n" + paymenthistory);
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "orderdetail", cascade = { CascadeType.ALL })
    public Set<Orderproduct> getOrderproducts() {
        return this.orderproducts;
    }

    public void setOrderproducts(Set<Orderproduct> orderproducts) {
        this.orderproducts = orderproducts;
    }

    @Column(name = "code", unique = true, nullable = false, length = 45)
    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(name = "cointotal", nullable = false)
    public int getCointotal() {
        return this.cointotal;
    }

    public void setCointotal(int cointotal) {
        this.cointotal = cointotal;
    }

    @Column(name = "transactionid", nullable = false, length = 50)
    public String getTransactionid() {
        return this.transactionid;
    }

    public void setTransactionid(String transactionid) {
        this.transactionid = transactionid;
    }

    @Override
    public String toString() {
        return String.format("Orderdetail<id: %s>", id);
    }
}
